$(function () {
    $("#khkctj_gsId").jygsjgwqxcombotree();
    $("#khkctj_gsId_chart").jygsjgwqxcombotree();
    //客户加载
    $("[name='khkctj_kh']").qyjgzcombobox();
    // 表格初始加载
    $('#khkctj_grid').datagrid({
        url: $.webPath() + "/fkyj/yjbb/khkctjList",
        toolbar: "#khkctj_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        singleSelect: true,
        showFooter: true,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'sbgsId', title: '客户id', hidden: true},
                {field: 'yq', title: '园区', align: 'center', width: 100},
                {field: 'kh', title: '客户', align: 'center', width: 70, sortable: true},
                {field: 'code', title: '物资编码', align: 'center', width: 70},
                {field: 'name', title: '物资名称', align: 'center', width: 70},
                {field: 'czggxh', title: '规格型号', align: 'center', width: 100},
                {field: 'dw', title: '单位', align: 'center', width: 70},
                {field: 'ck', title: '仓库', align: 'center', width: 70},
                {field: 'cd', title: '产地', align: 'center', width: 70},
                {field: 'jl', title: '重量', align: 'center', width: 70,
                    formatter: function (value) {
                        if (value != 0) {
                            return  value.toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {field: 'hz', title: '货值(元)', align: 'center', sortable: true, width: 120,
                    formatter: function (value) {
                        if (value != 0) {
                            return  value.toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    });

    // 查询按钮
    $("#btn_khkctj_search").click(function () {
        khkctj_search();
    });

    // 列表list
    $("#btn_khkctj_list").click(function () {
        khkctj_list();
    });

    // 图表按钮
    $("#btn_khkctj_chart").click(function () {
        khkctj_chart();
    });
});


// 客户物资与库存图表
function khkctj_chart() {
    var khChart = $("#khkctj_toolbar input[name='khkctj_kh']").val();
    var codeChart = $("#khkctj_toolbar input[name='khkctj_code']").val();
    var nameChart = $("#khkctj_toolbar input[name='khkctj_name']").val();
    var ggxhChart = $("#khkctj_toolbar input[name='khkctj_ggxh']").val();
    var cggsidChart = $("#khkctj_gsId_chart").combotree("getValue");
    var tj = $("input[name='khkctj_tj_name']").val();
    var reg = /^\d+$/;
    if (!tj.match(reg) && tj != -1) {
        $.messager.alert("提示", "请输入大于0的正整数！", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: $.webPath() + "/fkyj/yjbb/khkctjChart",
        dataType: "text",
        data: {khChart: khChart, codeChart: codeChart, nameChart: nameChart, ggxhChart: ggxhChart, cggsidChart: cggsidChart, tj: tj},
        beforeSend: function () {
            $.messager.progress();
        },
        success: function (result) {
            var dataJson = eval("(" + result + ")");
            $.messager.progress("close");
            if (null != dataJson.listXdata && null != dataJson.listYdata) {
                $('#khkctj_container').highcharts({
                    chart: {
                        type: 'column',
                        marginBottom: 80
                    },
                    title: {
                        text: '库存分析报表',
                        fontSize: '18px',
                        x: -20
                    },
                    xAxis: {
                        categories: dataJson.listXdata,
                        labels: {
                            rotation: -35,
                            style: {
                                fontSize: '13px',
                                fontFamily: 'Verdana, sans-serif',
                                marginTop:20
                            }
                        }
                    },
                    yAxis: {
                        min: 0,
                        title: {
                            text: '物资库存货值(元)'
                        },
                        tickPixelInterval: 25
                    },
                    legend: {
                        enabled: false
                    },
                    tooltip: {
                        formatter: function () {
                            return '<b>' + this.x + '</b><br/>' +
                                '库存货值: ' + Highcharts.numberFormat(this.y, 1) +
                                ' 元';
                        }
                    },
                    series: [
                        {
                            name: '客户库存货值',
                            data: dataJson.listYdata,
                            dataLabels: {
                                enabled: true,
                                rotation: -90,
                                color: '#FFFFFF',
                                align: 'right',
                                x: 4,
                                y: 10,
                                style: {
                                    fontSize: '10px',
                                    fontFamily: 'Verdana, sans-serif'
                                }
                            }
                        }
                    ]
                });
            }

        }
    });
    document.getElementById("layout_khkctj").style.display = "none"
    document.getElementById("div_khkctj_container").style.display = "";
}

// 客户库存列表
function khkctj_list() {
    document.getElementById("layout_khkctj").style.display = ""
    document.getElementById("div_khkctj_container").style.display = "none";
}

// 客户库存查询
function khkctj_search() {
    var kh = $("#khkctj_toolbar input[name='khkctj_kh']").val();
    var code = $("#khkctj_toolbar input[name='khkctj_code']").val();
    var name = $("#khkctj_toolbar input[name='khkctj_name']").val();
    var ggxh = $("#khkctj_toolbar input[name='khkctj_ggxh']").val();
    var cggsid = $("#khkctj_gsId").combotree("getValue");
    $('#khkctj_grid').datagrid('load',
        {
            kh: kh,
            code: code,
            name: name,
            ggxh: ggxh,
            cggsid: cggsid
        }
    );
}
